package com.example.myapplication;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;

public class CurrencyAdapter extends BaseAdapter {
    private final List<HashMap<String, String>> data;
    private final LayoutInflater inflater;

    public CurrencyAdapter(Context context, List<HashMap<String, String>> data) {
        this.data = data;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public HashMap<String, String> getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, parent, false);
            holder = new ViewHolder();
            holder.currencyText = convertView.findViewById(R.id.itemTitle);
            holder.rateText = convertView.findViewById(R.id.itemDetail);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> item = getItem(position);

        // 设置文本颜色
        holder.currencyText.setTextColor(Color.parseColor("#2196F3"));  // 蓝色
        holder.rateText.setTextColor(Color.parseColor("#F44336"));      // 红色

        // 绑定数据
        holder.currencyText.setText(item.get("currency"));
        holder.rateText.setText(item.get("rate"));

        return convertView;
    }

    static class ViewHolder {
        TextView currencyText;
        TextView rateText;
    }
}